<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

mm_showMyFileName( __FILE__ );

$itemid = @JRequest::getInt("Itemid",0);
$limit = @JRequest::getInt("limit",9);;
$limitstart = @JRequest::getInt("limitstart",0);
$page_temp = $limitstart;
$page = 0;

While($page_temp >= 0){
	$page_temp -= $limit;
	$page++;
}

$q = "SELECT * FROM #__vm_category WHERE `category_publish` = 'Y'";
$db = JFactory::getDBO();
$db->setQuery($q);
$category = $db->loadObjectList();

$category_id = @JRequest::getInt("category_id",$category[0]->category_id);

$db =& JFactory::getDBO();
/*$q = "SELECT a.`product_id`, a.`product_name`, a.`product_thumb_image`, a.`product_full_image`, a.`product_price`, a.`product_currency` FROM #__vm_product_category_xref b INNER JOIN (";
$q .= "SELECT pd.`product_id`, `product_name`, `product_thumb_image`, `product_full_image`, `product_price`, `product_currency` ";
$q .= "FROM #__vm_product pd inner join #__vm_product_price pd_p ";
$q .= "ON pd.product_id = pd_p.product_id ";
$q .= "WHERE pd.`product_publish` = 'Y' ";
$q .= ") a ON a.`product_id` = b.`product_id`";
$q .= "WHERE b.`category_id` = $category_id";*/

$q = "SELECT a.`product_id`, a.`product_name`, a.`product_thumb_image`, a.`product_full_image`, a.`product_s_desc` ";
$q.= "FROM #__vm_product a INNER JOIN #__vm_product_category_xref b ON a.`product_id` = b.`product_id` ";
$q.= "WHERE b.`category_id` = $category_id ";
$q.= "AND a.`product_publish` = 'Y' LIMIT $limitstart, $limit";

$db->setQuery($q);
$list = $db->loadObjectList();

$q = "SELECT a.`product_id`, a.`product_name`, a.`product_thumb_image`, a.`product_full_image`, a.`product_s_desc` ";
$q.= "FROM #__vm_product a INNER JOIN #__vm_product_category_xref b ON a.`product_id` = b.`product_id` ";
$q.= "WHERE b.`category_id` = $category_id ";
$q.= "AND a.`product_publish` = 'Y' ";

$db->setQuery($q);
$total = $db->loadObjectList();
$totalpage = 0;
$t_temp = count($total);
while($t_temp >= 0){
	$totalpage++;
	$t_temp -= $limit;
}



$tpl = vmTemplate::getInstance();
$tpl->set("limit",$limit);
$tpl->set("limitstart",$limitstart);
$tpl->set("category",$category);
$tpl->set("category_id",$category_id);
$tpl->set("list",$list);
$tpl->set("Itemid",$itemid);
$tpl->set("totalpage",$totalpage);
$tpl->set("page",$page);

$flypage = "shop.list.tpl";
echo $tpl->fetch( '/pages/'.$flypage . '.php' );
?>